package com.jxk.provider1.filter;

import com.jxk.common.util.JwtUtils;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HttpBasicAuthorizeFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setCharacterEncoding("UTF-8");
        httpResponse.setContentType("application/json; charset=utf-8");
        if(JwtUtils.checkToken(httpRequest)){
            String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(httpRequest);
            if (!StringUtils.isEmpty(memberIdByJwtToken)){
                chain.doFilter(httpRequest, response);
            }
        }
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write("非法请求".getBytes());
    }
}
